<?php
/*
---------------------------------------------------------------------------
admin/functions.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
function yes_no($bool) {
	switch ($bool) {
		case 0:
			return 'No';
			break;
		case 1:
			return 'Yes';
			break;
	}
}

function show_hosts() {
	$location_result = run_query("SELECT locations.id, locations.name FROM locations, hosts WHERE locations.id = hosts.location_id GROUP BY hosts.location_id ORDER BY COUNT(hosts.id) DESC");
	if (mysql_num_rows($location_result) != NULL) {
		while(list($location_id, $location_name) = mysql_fetch_array($location_result))	{
			echo '<div class="loc_status"><h3>', $location_name, '</h3><table width="500px">';
			$group_result = run_query("SELECT id, name FROM groups ORDER BY name ASC");
			if (mysql_num_rows($group_result) != NULL) {
				while(list($group_id, $group_name) = mysql_fetch_array($group_result))	{
					$host_result = run_query("SELECT id, name, enable_monitoring FROM hosts WHERE location_id = '$location_id' AND group_id = '$group_id' ORDER BY name ASC");
					if (mysql_num_rows($host_result) != NULL) {
						echo '<tr><th colspan=4>', $group_name, '</th></tr>
						<tr><th width="100px">Name:</th><th>Checks:<th width="40px">Enabled:</th><th width="70px">Options:</th></tr>';
						while(list($id, $name, $enabled) = mysql_fetch_array($host_result))	{
							echo '<tr><td>', $name, '</td><td>';
							$trigger_result = run_query("SELECT triggers.name FROM triggers, hosts_triggers WHERE hosts_triggers.host_id = '$id' AND hosts_triggers.trigger_id = triggers.id ORDER BY triggers.name ASC");
							if (mysql_num_rows($trigger_result) != NULL) {
								$firstrow = TRUE;
								while(list($trigger_name) = mysql_fetch_array($trigger_result)) {
									if ($firstrow == TRUE) {
										echo $trigger_name;
										$firstrow = FALSE;
									}
									else
										echo ', ', $trigger_name;
								}
							}
							echo '</td><td>', yes_no($enabled), '</td><td><a href="hosts.php?action=edit&id=', $id, '">Edit</a> / <a href="hosts.php?action=confirm&name=', $name, '&id=', $id, '">Delete<a/></td></tr>';
						}
					}
				}
			}
			else
				echo 'No Groups Found, <a href="groups.php">Groups</a>';
			echo '</table><br />&nbsp;</div>';
		}
	}
	else
		echo 'No Locations Found, <a href="locations.php">Locations</a>';
}

function add_host_form($name, $dns_name_or_ip, $location, $group, $triggers) {
	echo '<form method="post" action="hosts.php?action=add" enctype="multipart/form-data">
	<table width="500" border="0" cellpadding="1" cellspacing="1">
	<tr><td align="right">Enable Monitoring:</td><td><input type="checkbox" name="enable" value="1" checked="checked" /></td></tr>
	<tr><td align="right">Descriptive Name:</td><td><input type="text" name="name" value="', $name, '" /></td></tr>
	<tr><td align="right">DNS Name or IP Address:</td><td><input type="text" name="dns_name_or_ip" value="', $dns_name_or_ip, '" /></td></tr>
	<tr><td align="right">Location:</td>
	<td><select name="location">';
	$result  = run_query("SELECT id, name FROM locations ORDER BY name ASC");
	if(mysql_num_rows($result) != 0) {
		while(list($id, $name) = mysql_fetch_array($result)) {
			echo '<option value="', $id;
			if ($id == $location)
				echo ' selected="selected"';
			echo '">', $name, '</option>';
		}
	}
	echo '</select></td></tr>
	<tr><td align="right">Group:</td>
	<td><select name="group">';
	$result  = run_query("SELECT id, name FROM groups ORDER BY name ASC");
	if(mysql_num_rows($result) != 0) {
		while(list($id, $name) = mysql_fetch_array($result)) {
			echo '<option value="', $id;
			if ($id == $group)
				echo ' selected="selected"';
			echo '">', $name, '</option>';
		}
	}
	echo '</select></td></tr>';
	$check_categories = run_query("SELECT id, name FROM check_categories ORDER BY id ASC");
	if(mysql_num_rows($check_categories) != NULL) {
		while(list($cat_id, $cat_name) = mysql_fetch_array($check_categories)) {
			echo '<tr><td align="right">', $cat_name, ' Triggers:</td><td>';
			$result  = run_query("SELECT triggers.id, triggers.name, triggers.frequency FROM triggers, checks WHERE triggers.check_id = checks.id AND checks.category_id = $cat_id ORDER BY name ASC");
			if(mysql_num_rows($result) != NULL) {
				while(list($id, $name, $frequency) = mysql_fetch_array($result)) {
					echo '<input type="checkbox" name="trigger[]" value="', $id, '" /> ', $name, ' (', $frequency, ' min)<br />';
				}
			}
		}
	}
	echo '<tr><td colspan="2" align="center"><input name="add" type="submit" class="box" value=" Add "></td></tr>
	</table>
	</form>';
}

function edit_host_form($id) {
	$result = run_query("SELECT name, dns_name_or_ip, location_id, group_id, enable_monitoring FROM hosts WHERE id = '$id' LIMIT 1");
	if(mysql_num_rows($result) != 0) {
		list($name, $dns_name_or_ip, $location_id, $group_id, $enabled) = mysql_fetch_array($result);
		echo '<form method="post" action="hosts.php?action=edit" enctype="multipart/form-data">
		<input type="hidden" name="id" value="', $id, '" />
		<table width="500" border="0" cellpadding="1" cellspacing="1">
		<tr><td align="right">Enable Monitoring:</td><td><input type="checkbox" name="enable" value="1"';
		if ($enabled == 1)
			echo ' checked="checked" ';
		echo '/></td></tr>
		<tr><td align="right">Name:</td><td><input type="text" name="name" value="', $name, '" /></td></tr>
		<tr><td align="right">DNS Name or IP Address:</td><td><input type="text" name="dns_name_or_ip" value="', $dns_name_or_ip, '" /></td></tr>
		<tr><td align="right">Location:</td>
		<td><select name="location">';
		$locresult  = run_query("SELECT id, name FROM locations ORDER BY name ASC");
		if(mysql_num_rows($locresult) != 0) {
			while(list($locid, $locname) = mysql_fetch_array($locresult)) {
				echo '<option value="', $locid, '"';
				if ($locid == $location_id)
					echo ' selected="selected"';
				echo '">', $locname, '</option>';
			}
		}
		echo '</select></td></tr>
		<tr><td align="right">Group:</td>
		<td><select name="group">';
		$gpresult  = run_query("SELECT id, name FROM groups ORDER BY name ASC");
		if(mysql_num_rows($gpresult) != 0) {
			while(list($gpid, $gpname) = mysql_fetch_array($gpresult)) {
				echo '<option value="', $gpid, '"';
				if ($gpid == $group_id)
					echo ' selected="selected"';
				echo '">', $gpname, '</option>';
			}
		}
		echo '</select></td></tr>';
		$ctresult  = run_query("SELECT trigger_id FROM hosts_triggers WHERE host_id = '$id'");
		if(mysql_num_rows($ctresult) != 0) {
			while($row = mysql_fetch_array($ctresult))
				$current_triggers[] = $row[0];
		}
		else
			$current_triggers[] = NULL;
		$check_categories = run_query("SELECT id, name FROM check_categories ORDER BY id ASC");
		if(mysql_num_rows($check_categories) != NULL) {
			while(list($cat_id, $cat_name) = mysql_fetch_array($check_categories)) {
				echo '<tr><td align="right">', $cat_name, ' Triggers:</td><td>';
				$result  = run_query("SELECT triggers.id, triggers.name, triggers.frequency FROM triggers, checks WHERE triggers.check_id = checks.id AND checks.category_id = $cat_id ORDER BY name ASC");
				if(mysql_num_rows($result) != NULL) {
					while(list($tid, $tname, $tfrequency) = mysql_fetch_array($result)) {
						echo '<input type="checkbox" name="trigger[]" value="', $tid, '"';
						foreach ($current_triggers as $trig_id) {
							if ($tid == $trig_id)
								echo ' checked="checked"';
						}
						echo ' /> ', $tname, '(', $tfrequency, ' min)<br />';
					}
				}
			}
		}
		echo '<tr><td colspan="2" align="center"><input name="save" type="submit" class="box" value=" Save "></td></tr>
		</table>
		</form>';
	}
}

function add_location_form() {
	echo '<form method="post" action="locations.php?action=add" enctype="multipart/form-data">
	<table width="500" border="0" cellpadding="1" cellspacing="1">
	<tr><td align="right">Name:</td><td><input type="text" name="name" value="', $name, '" /></td></tr>
	<tr><td colspan="2" align="center"><input name="add" type="submit" class="box" value=" Add "></td></tr>
	</table>
	</form>';
}

function edit_location_form($id) {
	$result = run_query("SELECT name FROM locations WHERE id = '$id' LIMIT 1");
	list($name) = mysql_fetch_array($result);
	echo '<form method="post" action="locations.php?action=edit" enctype="multipart/form-data">
	<input type="hidden" name="id" value="', $id, '" />
	<table width="500" border="0" cellpadding="1" cellspacing="1">
	<tr><td align="right">Name:</td><td><input type="text" name="name" value="', $name, '" /></td></tr>
	<tr><td colspan="2" align="center"><input name="save" type="submit" class="box" value=" Save "></td></tr>
	</table>
	</form>';
}

function add_group_form() {
	echo '<form method="post" action="groups.php?action=add" enctype="multipart/form-data">
	<table width="500" border="0" cellpadding="1" cellspacing="1">
	<tr><td align="right">Name:</td><td><input type="text" name="name" value="', $name, '" /></td></tr>
	<tr><td colspan="2" align="center"><input name="add" type="submit" class="box" value=" Add "></td></tr>
	</table>
	</form>';
}

function edit_group_form($id) {
	$result = run_query("SELECT name FROM groups WHERE id = '$id' LIMIT 1");
	list($name) = mysql_fetch_array($result);
	echo '<form method="post" action="groups.php?action=edit" enctype="multipart/form-data">
	<input type="hidden" name="id" value="', $id, '" />
	<table width="500" border="0" cellpadding="1" cellspacing="1">
	<tr><td align="right">Name:</td><td><input type="text" name="name" value="', $name, '" /></td></tr>
	<tr><td colspan="2" align="center"><input name="save" type="submit" class="box" value=" Save "></td></tr>
	</table>
	</form>';
}

function add_trigger_page1() {
	global $frequency;
	echo '<form method="post" action="triggers.php?action=add" enctype="multipart/form-data">
	<table width="500">
	<tr><td align="right">Which Check Would you like to base this trigger on?:</td>
	<td><select name="check_id">';
	$result  = run_query("SELECT id, name FROM checks ORDER BY name ASC");
	if(mysql_num_rows($result) != 0) {
		while(list($id, $name) = mysql_fetch_array($result))
			echo '<option value="', $id, '">', $name, '</option>';
	}
	echo '</select></td></tr>
	<tr><td colspan="2" align="center"><input type="submit" value=" Next "></td></tr>
	</table>
	</form>';
}
function add_trigger_page2($check_id) {
	global $frequency, $wmi_user, $wmi_password;
	$result  = run_query("SELECT name, available_args, args_name, args_type, args_default, manpage FROM checks WHERE id = '$check_id' LIMIT 1");
	if(mysql_num_rows($result) != 0) {
		list($check_name, $available_args, $args_name, $args_type, $args_default, $manpage) = mysql_fetch_array($result);
		$available_args = explode("|", $available_args);
		$args_name = explode("|", $args_name);
		$args_type = explode("|", $args_type);
		$args_default = explode("|", $args_default);
		echo '<form method="post" action="triggers.php?action=add" enctype="multipart/form-data">
		<input type="hidden" name="check_id" value="', $check_id, '" />
		<table width="500" border="0" cellpadding="1" cellspacing="1">
		<tr><td align="right">Name:</td><td><input type="text" name="name" /></td></tr>
		<tr><td align="right">Frequency(Minutes):</td>
		<td><select name="frequency">';
		foreach ($frequency as $value)
			echo '<option value="', $value, '">', $value, '</option>';
		echo '</select></td></tr>
		<tr><td align="right">Send Warning Email:</td><td><input type="checkbox" name="send_warning" value="1" /></td></tr>
		<tr><td align="right">Send Critical Email:</td><td><input type="checkbox" name="send_critical" value="1" checked="checked" /></td></tr>
		<tr><th colspan="2">', $check_name, ' Options</th></tr>';
		foreach ($available_args as $key => $arg) {
			echo '<tr><td align="right">', $args_name[$key], '</td><td>';
			switch ($args_type[$key]) {
				case 'ms,%':
					$arr = explode(",", $args_default[$key]);
					echo '<input type="text" name="args[', $arg, '][]" value="', $arr[0], '" />milliseconds or <select name="args[', $arg, '][]">';
					$i = 5;
					while ($i <= 100) {
						echo '<option value="', $i, '%"';
						if ($i == intval($arr[1]))
							echo ' selected="selected"';
						echo '>', $i, '%</option>';
						$i += 5;
					}
					echo '</select> Loss</td></tr>';
					break;
				case 'MB,%':
					$arr = explode(",", $args_default[$key]);
					echo '<input type="text" name="args[', $arg, '][]" value="', $arr[0], '" />MB or <select name="args[', $arg, '][]">';
					$i = 5;
					while ($i <= 100) {
						echo '<option value="', $i, '%"';
						if ($i == intval($arr[1]))
							echo ' selected="selected"';
						echo '>', $i, '%</option>';
						$i += 5;
					}
					echo '</select></td></tr>';
					break;
				case 'GB,%':
					$arr = explode(",", $args_default[$key]);
					echo '<input type="text" name="args[', $arg, '][]" value="', $arr[0], '" />GB or <select name="args[', $arg, '][]">';
					$i = 5;
					while ($i <= 100) {
						echo '<option value="', $i, '%"';
						if ($i == intval($arr[1]))
							echo ' selected="selected"';
						echo '>', $i, '%</option>';
						$i += 5;
					}
					echo '</select></td></tr>';
					break;
				case 'checkbox':
					echo '<input type="checkbox" name="args[', $arg, ']" value="', $args_default[$key], '" />';
					break;
				case 'int':
				case 'string':
					echo '<input type="text" name="args[', $arg, ']" value="', $args_default[$key], '" />';
					break;
				case 'password':
					echo '<input type="password" name="args[', $arg, ']" value="', $args_default[$key], '" />';
					break;
				default:
					echo '<input type="text" name="args[', $arg, ']" value="', $args_default[$key], '" />', $args_type[$key];
					break;
			}
			echo '</td></tr>';
		}
		echo '<tr><th colspan="2">Advanced Options</th></tr>
		<tr><td align="right">Additional Arguments:</td><td><input type="text" name="add_args" /></td></tr>
		<tr><td align="right">More Information:</td><td><a href="', $manpage, '" target="_blank">', $check_name, ' Information</a></td></tr>
		<tr><td colspan="2" align="center"><input type="submit" value=" Add "></td></tr>
		</table>
		</form>';
	}
}

function edit_trigger($id) {
	global $frequency, $wmi_user, $wmi_password;
	$trigger_result = run_query("SELECT name, check_id, arguments, frequency, send_warning_email, send_critical_email FROM triggers WHERE id = '$id'");
	list($name, $check_id, $arguments, $trig_frequency, $send_warning, $send_critical) = mysql_fetch_array($trigger_result);
	$check_result  = run_query("SELECT name, available_args, args_name, args_type, manpage FROM checks WHERE id = '$check_id' LIMIT 1");
	if(mysql_num_rows($check_result) != 0) {
		list($check_name, $available_args, $args_name, $args_type, $manpage) = mysql_fetch_array($check_result);
		$available_args = explode("|", $available_args);
		$args_name = explode("|", $args_name);
		$args_type = explode("|", $args_type);
		$temp_args = explode("-", $arguments);
		foreach ($temp_args as $value) {
			list($arg, $data) = explode(" ", $value, 2);
			$current_args[$arg] = $data;
		}
		echo '<form method="post" action="triggers.php?action=edit" enctype="multipart/form-data">
		<input type="hidden" name="id" value="', $id, '" />
		<table width="500" border="0" cellpadding="1" cellspacing="1">
		<tr><td align="right">Name:</td><td><input type="text" name="name" value="', $name, '" /></td></tr>
		<tr><td align="right">Frequency(Minutes):</td>
		<td><select name="frequency">';
		foreach ($frequency as $value) {
			echo '<option value="', $value, '"';
			if ($value == $trig_frequency)
				echo ' selected="selected"';
			echo '>', $value, '</option>';
		}
		echo '</select></td></tr>
		<tr><td align="right">Send Warning Email:</td><td><input type="checkbox" name="send_warning" value="1"';
		if ($send_warning == 1)
			echo ' checked="checked"';
		echo ' /></td></tr>
		<tr><td align="right">Send Critical Email:</td><td><input type="checkbox" name="send_critical" value="1"';
		if ($send_critical == 1)
			echo ' checked="checked"';
		echo ' /></td></tr>
		<tr><th colspan="2">', $check_name, ' Options</th></tr>';
		foreach ($available_args as $key => $arg) {
			echo '<tr><td align="right">', $args_name[$key], '</td><td>';
			switch ($args_type[$key]) {
				case 'ms,%':
					$arr = explode(",", $current_args[$arg]);
					echo '<input type="text" name="args[', $arg, '][]" value="', $arr[0], '" />milliseconds or <select name="args[', $arg, '][]">';
					$i = 5;
					while ($i <= 100) {
						echo '<option value="', $i, '%"';
						if ($i == intval($arr[1]))
							echo ' selected="selected"';
						echo '>', $i, '%</option>';
						$i += 5;
					}
					echo '</select> Loss</td></tr>';
					break;
				case 'MB,%':
					$arr = explode(",", $current_args[$arg]);
					echo '<input type="text" name="args[', $arg, '][]" value="', $arr[0], '" />MB or <select name="args[', $arg, '][]">';
					$i = 5;
					while ($i <= 100) {
						echo '<option value="', $i, '%"';
						if ($i == intval($arr[1]))
							echo ' selected="selected"';
						echo '>', $i, '%</option>';
						$i += 5;
					}
					echo '</select></td></tr>';
					break;
				case 'GB,%':
					$arr = explode(",", $current_args[$arg]);
					echo '<input type="text" name="args[', $arg, '][]" value="', $arr[0], '" />GB or <select name="args[', $arg, '][]">';
					$i = 5;
					while ($i <= 100) {
						echo '<option value="', $i, '%"';
						if ($i == intval($arr[1]))
							echo ' selected="selected"';
						echo '>', $i, '%</option>';
						$i += 5;
					}
					echo '</select></td></tr>';
					break;
				case 'checkbox':
					echo '<input type="checkbox" name="args[', $arg, ']" value="', $current_args[$arg], '" />';
					break;
				case 'int':
				case 'string':
					echo '<input type="text" name="args[', $arg, ']" value="', $current_args[$arg], '" />';
					break;
				case 'password':
					echo '<input type="password" name="args[', $arg, ']" value="', $current_args[$arg], '" />';
					break;
				default:
					echo '<input type="text" name="args[', $arg, ']" value="', $current_args[$arg], '" />', $args_type[$key];
					break;
			}
			echo '</td></tr>';
			unset($current_args[$arg]);
		}
		$add_args = '';
		foreach ($current_args as $key => $data) {
			if ($data != '')
				$add_args .= ' -' . $key . ' ' . $data;
		}
		echo '<tr><th colspan="2">Advanced Options</th></tr>
		<tr><td align="right">Additional Arguments:</td><td><input type="text" name="add_args" value="', $add_args, '" /></td></tr>
		<tr><td align="right">More Information:</td><td><a href="', $manpage, '" target="_blank">', $check_name, ' Information</a></td></tr>
		<tr><td colspan="2" align="center"><input name="save" type="submit" class="box" value=" Save "></td></tr>
		</table>
		</form>';
	}
}
?>